package inPutStreamAndoutPutStream;

import java.io.*;
import java.util.Scanner;

public class ReadAndWrite {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入文件路径");
        String src = input.next();
        System.out.println("请输入复制到那里去");
        String dest = input.next();
        try {
            copy(src,dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy(String src,String dest) throws IOException {
        InputStream in=null;
        OutputStream out=null;
        try {
            in=new FileInputStream(src);
            out=new FileOutputStream(dest);

            int i;
            byte[] bytes=new byte[1024];
            while ((i=in.read(bytes))!= -1){
                out.write(bytes,0,i);
            }
            out.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           if (out!=null)out.close();
           if (in!=null)in.close();
        }
    }

    private static void copy2(String src,String dest) throws IOException {
        InputStream in=null;
        OutputStream out=null;

        try {
            in=new FileInputStream(src);
            out=new FileOutputStream(dest);

            int i;
            while ((i=in.read())!=-1){
                out.write(i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out!=null)out.close();
            if (in!=null)in.close();
        }
    }
}
